package com.copro.chemdb.stat.rule.export;

import java.util.List;
import java.util.Map;

import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.ihelpuoo.util.Properties;

import com.copro.chemdb.stat.ExportRule;
import com.copro.chemdb.stat.StatResult;
import com.copro.chemdb.subject.Subject;
import com.copro.simple.Application;
import com.copro.simple.SimpleException;

public class Rule7 implements ExportRule {

	@SuppressWarnings("unchecked")
	@Override
	public void doExport(HSSFWorkbook wb, Subject subject, StatResult re,
			short customStyle, Application app) throws SimpleException {
		Map<String, Object> result = (Map<String, Object>) re.getResult();
		int resultCount = Integer.parseInt((String) result.get("count"));
		// create new sheet
		HSSFSheet sheet = wb.createSheet("按省份统计问卷数量和所占比例");
		// set the header
		HSSFRow row = sheet.createRow(0);
		CellUtil.createCell(row, 0, "省份", wb
				.getCellStyleAt((short) (customStyle + StyleHeader)));
		CellUtil.createCell(row, 1, "数量", wb
				.getCellStyleAt((short) (customStyle + StyleHeader)));
		CellUtil.createCell(row, 2, "所占比例", wb
				.getCellStyleAt((short) (customStyle + StyleHeader)));
		// set the data body
		List<List<String>> provs = (List<List<String>>) result.get("province");
		for (int i = 0; i < provs.size(); i++) {
			List<String> cells = provs.get(i);
			row = sheet.createRow(i + 1);
			CellUtil.createCell(row, 0, cells.get(0));
			int num = Integer.parseInt(cells.get(1));
			CellUtil.createCell(row, 1, num, wb
					.getCellStyleAt((short) (customStyle + StyleInt)));
			double percent = resultCount == 0 ? 0 : (double) num / resultCount;
			CellUtil.createCell(row, 2, percent, wb
					.getCellStyleAt((short) (customStyle + StylePercent)));
		}

		// set the footer body
		row = sheet.createRow(provs.size() + 1);
		CellUtil.createCell(row, 0, "合计", wb
				.getCellStyleAt((short) (customStyle + StyleHeader)));
		CellUtil.createCell(row, 1, resultCount, wb
				.getCellStyleAt((short) (customStyle + StyleHeaderInt)));
		CellUtil.createCell(row, 2, 1, wb
				.getCellStyleAt((short) (customStyle + StyleHeaderPercent)));

		// create new sheet
		sheet = wb.createSheet("按地区统计问卷数量和所占比例");
		// set the header
		row = sheet.createRow(0);
		CellUtil.createCell(row, 0, "地区", wb
				.getCellStyleAt((short) (customStyle + StyleHeader)));
		CellUtil.createCell(row, 1, "数量", wb
				.getCellStyleAt((short) (customStyle + StyleHeader)));
		CellUtil.createCell(row, 2, "所占比例", wb
				.getCellStyleAt((short) (customStyle + StyleHeader)));
		// set the data body
		List<List<String>> parts = (List<List<String>>) result.get("parts");
		for (int i = 0; i < parts.size(); i++) {
			List<String> cells = parts.get(i);
			row = sheet.createRow(i + 1);
			CellUtil.createCell(row, 0, cells.get(0));
			int num = Integer.parseInt(cells.get(1));
			CellUtil.createCell(row, 1, num, wb
					.getCellStyleAt((short) (customStyle + StyleInt)));
			double percent = resultCount == 0 ? 0 : (double) num / resultCount;
			CellUtil.createCell(row, 2, percent, wb
					.getCellStyleAt((short) (customStyle + StylePercent)));
		}

		// set the footer body
		row = sheet.createRow(parts.size() + 1);
		CellUtil.createCell(row, 0, "合计", wb
				.getCellStyleAt((short) (customStyle + StyleHeader)));
		CellUtil.createCell(row, 1, resultCount, wb
				.getCellStyleAt((short) (customStyle + StyleHeaderInt)));
		CellUtil.createCell(row, 2, 1, wb
				.getCellStyleAt((short) (customStyle + StyleHeaderPercent)));
	}

	@Override
	public void setup(Properties conf) {
	}

}
